﻿using LY.Business.ServiceImpl;
using LY.Entity;
using LY.Entity.Jsons;
using LY.Entity.PageSearch;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class ModuleController : Controller
    {
        //
        // GET: /Module/
        private IModuleService _mservice;
        public ModuleController(IModuleService mService)
        {
            _mservice = mService; 
        }
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 模块列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ModuleList(FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];

            PagedList<S_Module> pList = _mservice.QueryModuleList(view);
            JsonQTable fdata = JsonQTable.ConvertFromPagedList<S_Module>(pList, colkey, colsinfo.Split(','));
            return Json(fdata);
        }

        /// <summary>
        /// 用户可见模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string NewUserModuleList(int id)
        {
            ModuleService mservice=new ModuleService();
            List<S_Module> list =mservice.QueryUserModuleList(id);
            string module = "";
            int newmodulelayer = 0;
            int oldmodulelayer = 0;
            string oldmodulepath = "";
            foreach (var item in list)
            {
                newmodulelayer = Convert.ToInt32(item.Layer);
                if (item.Layer != 0)
                {
                    if (item.Layer != 1)
                    {

                        //if (LY.Common.Utility.IsURL(item.Url))
                        //{

                        //}

                        if (newmodulelayer > oldmodulelayer)
                            {
                                module += "<ul><li><a href=\"" + item.Url + "\" target=\"mainframe\">" + item.Name + "</a></li>";
                            }
                            else
                            {
                                module += "<li><a href=\"" + item.Url + "\"  target=\"mainframe\">" + item.Name + "</a></li>";
                            }
                    }
                    else
                    {
                        if (newmodulelayer < oldmodulelayer)
                        {
                            module += "</ul>";
                        }
                        module += "<h3 id=\"module"+item.ID+"\">" + item.Name + "</h3>";
                        oldmodulepath = item.Path;
                    }
                }
                oldmodulelayer = Convert.ToInt32(item.Layer);
            }
            module += "</ul>";

            return module;
        }
        //****编辑 新增 模块信息*******//
        public ActionResult ModuleEdit(int? id)
        {

            S_Module invest = null;
            if (id.HasValue)
            {
                string SubModuleId = "";
                invest = _mservice.GetModule(id.Value);
                List<S_Module> list = _mservice.GetModuleSub(id.Value);
                foreach (var item in list)
                {
                    SubModuleId += item.ID.ToString() + ",";
                }
                if (SubModuleId != "")
                {
                    SubModuleId = SubModuleId.Substring(0, SubModuleId.Length - 1);
                }
                ViewBag.SubModuleId = SubModuleId;
                //ViewBag.SubModuleId = _mservice.GetModuleSub(id.Value);
            }
            else
            {
                invest = new S_Module();
            }
            return View(invest);
        }

        /// <summary>
        /// 保存模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="module"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ModuleSave(int? id, S_Module module, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";
                if (!Validate(module, out errmsg))
                {
                    rMsg.Status = -1;
                    rMsg.Message = errmsg;
                    return Json(rMsg);
                }
                if (id == 0)
                {

                }

                int ret = _mservice.SaveModule(module);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        private static bool Validate(S_Module module, out string errmsg)
        {
            errmsg = "";
            if (module == null)
            {
                errmsg = "参数错误！";
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ModuleDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                    return Json(rMsg);
                }

                int ret = _mservice.DeleteModule(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

    }
}
